﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace TestPlatform.Core.Impl
{
    internal class TestRunner : ITestRunner
    {
        public TestResult Run(IEnumerable<ITestPerformer> performers, IEnumerable<int> domain)
        {
            TestResult res = new TestResult { Result = new Dictionary<ITestPerformer, Dictionary<int, TimeSpan>>() };

            foreach (var p in performers)
            {
                Console.WriteLine("Starting performer: {0}, {1}", p.UnderlyinEngine, p.OperationType);

                Dictionary<int, TimeSpan> partialResult = new Dictionary<int, TimeSpan>();
                res.Result.Add(p, partialResult);

                foreach (var i in domain)
                {
                    Console.WriteLine("Starting test with instance size: {0}", i);

                    p.Initialize(i);

                    Stopwatch sw = Stopwatch.StartNew();
                    p.PerformTest();
                    sw.Stop();

                    p.Cleanup();

                    Console.WriteLine("Test finished in: {0}", sw.Elapsed);

                    partialResult.Add(i, sw.Elapsed);
                }
            }

            return res;
        }
    }
}
